package com.doupi.partner.rpc.biz;

import com.doupi.partner.constant.PartnerTypeEnum;
import com.doupi.partner.constant.ProfitSourceEnum;
import com.doupi.partner.constant.UserTypeEnum;
import com.doupi.partner.dal.model.SystemConfig;
import com.doupi.partner.model.BusinessRelation;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.model.Profit;
import com.doupi.partner.service.BusinessRelationService;
import com.doupi.partner.service.PartnerRelationService;
import com.doupi.partner.service.ProfitService;
import com.doupi.partner.service.SystemConfigService;
import com.doupi.partner.util.AssertUtils;
import com.doupi.partner.util.ObjectUtils;
import com.doupi.partner.util.constant.UtilConstants;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 收益
 *
 * @author kxj
 * @version 1.0 @ 2017-04-17 15:11
 **/
@Component("profitBiz")
public class ProfitBizImpl implements ProfitBiz {
	private static final Logger LOGGER = LoggerFactory.getLogger(ProfitBizImpl.class);
	// 一级广告收益
	private static final String SUPERIOR_PARTNER_ADVERTISE = "superior_partner_advertise";

	// 二级广告收益
	private static final String SECOND_PARTNER_ADVERTISE = "second_partner_advertise";

	private final BigDecimal SUPERIOR_PARTNER_ADVERTISE_BD;

	private final BigDecimal SECOND_PARTNER_ADVERTISE_BD;

	private final ProfitService profitService;
	private final BusinessRelationService businessRelationService;
	private final PartnerRelationService partnerRelationService;

	@Autowired
	public ProfitBizImpl(ProfitService profitService, BusinessRelationService businessRelationService, SystemConfigService systemConfigService, PartnerRelationService partnerRelationService) {
		this.profitService = profitService;
		this.businessRelationService = businessRelationService;
		this.partnerRelationService = partnerRelationService;

		// 1. 查询1级城市合伙人 提成
		SystemConfig systemConfig = systemConfigService.selectByPrimaryKey(SUPERIOR_PARTNER_ADVERTISE);
		SUPERIOR_PARTNER_ADVERTISE_BD = new BigDecimal(systemConfig.getValue());

		// 2. 查询2级合伙人 提成
		systemConfig = systemConfigService.selectByPrimaryKey(SECOND_PARTNER_ADVERTISE);
		SECOND_PARTNER_ADVERTISE_BD = new BigDecimal(systemConfig.getValue());
	}

	@Override
	public synchronized int addProfit(Profit profit) {
		LOGGER.info("[partner-rpc] - 收到收益{}", profit);
		// TODO 做些校验 立即有效，应该是先无效，后对账系统跑批有效

		// 0. 校验是否处理过了
		Profit profitParam = new Profit();
		profitParam.setBusinessId(profit.getBusinessId());
		profitParam.setCouponRecordId(profit.getCouponRecordId());
		int count = profitService.countProfit(profitParam);
		if (count > 0) {
			return 0;
		}

		// 1. 查询商家关系
		BusinessRelation busRelation = new BusinessRelation();
		busRelation.setBusinessId(profit.getBusinessId());
		List<BusinessRelation> businessRelationList = businessRelationService.queryBusinessRelations(busRelation, new RowBounds(0, 1));
		BusinessRelation businessRelation = businessRelationList.get(0);
		LOGGER.info("[partner-rpc] - 查询到商家关系{}", businessRelation);

		// 2. 查询商家所属合伙人
		PartnerRelation partnerRelation = partnerRelationService.selectByAdministratorId(businessRelation.getPartnerAdministratorId());
		LOGGER.info("[partner-rpc] - 查询到合伙人关系{}", partnerRelation);
		PartnerTypeEnum partnerType = PartnerTypeEnum.getPartnerTypeByCode(partnerRelation.getType());
		AssertUtils.assertNotNull(partnerType, "合伙人类型不对");
		BigDecimal realAmount = SUPERIOR_PARTNER_ADVERTISE_BD.multiply(profit.getTotalAmount());
		switch (partnerType) {
		case SECOND: // 二级合伙人
			// 一级合伙人收益
			Profit superiorProfit = new Profit();
			ObjectUtils.copyProperties(superiorProfit, profit);

			// 1. 添加二级合伙人的收益
			LOGGER.info("[partner-rpc] - 插入二级合伙人收益");
			BigDecimal superiorAmount = realAmount.multiply(SECOND_PARTNER_ADVERTISE_BD);
			profit.setFee(BigDecimal.ZERO);
			profit.setType(ProfitSourceEnum.ADVERTISE_PROFIT.getCode());
			profit.setPlatformFee(BigDecimal.ZERO);
			profit.setRealAmount(realAmount.subtract(superiorAmount));
			profit.setAdministratorId(partnerRelation.getAdministratorId());
			profit.setSuperiorPartnerAdministratorId(partnerRelation.getSuperiorAdministratorId());
			profit.setCityAdministratorId(partnerRelation.getCityAdministratorId());
			profit.setSubstationAdministratorId(partnerRelation.getSubstationAdministratorId());
			profit.setCity(partnerRelation.getCity());
			profit.setBusinessAdministratorId(businessRelation.getAdministratorId());
			if (null == profit.getUserType() || profit.getUserType() == UserTypeEnum.PLATFORM.getCode()) {
				profit.setStatus(UtilConstants.YES) ;
			} else {
				profit.setStatus(UtilConstants.YES);
			}
			int ret = profitService.insert(profit);
			if (ret > 0) {
				// 2. 添加一级合伙人的收益
				LOGGER.info("[partner-rpc] - 插入一级合伙人二级收益");
				PartnerRelation superiorPartnerRelation = partnerRelationService.selectByAdministratorId(partnerRelation.getSuperiorAdministratorId());
				superiorProfit.setFee(BigDecimal.ZERO);
				superiorProfit.setType(ProfitSourceEnum.SECOND_ADVERTISE_PROFIT.getCode());
				superiorProfit.setPlatformFee(BigDecimal.ZERO);
				superiorProfit.setRealAmount(superiorAmount);
				superiorProfit.setPartnerAdministratorId(profit.getAdministratorId());
				superiorProfit.setAdministratorId(superiorPartnerRelation.getAdministratorId());
				superiorProfit.setBusinessAdministratorId(businessRelation.getAdministratorId());
				superiorProfit.setTotalAmount(profit.getTotalAmount());
				superiorProfit.setSuperiorPartnerAdministratorId(superiorPartnerRelation.getSuperiorAdministratorId());
				superiorProfit.setCityAdministratorId(superiorPartnerRelation.getCityAdministratorId());
				superiorProfit.setSubstationAdministratorId(superiorPartnerRelation.getSubstationAdministratorId());
				superiorProfit.setCity(superiorPartnerRelation.getCity());
				if (null == superiorProfit.getUserType() || superiorProfit.getUserType() == UserTypeEnum.PLATFORM.getCode()) {
					superiorProfit.setStatus(UtilConstants.YES);
				} else {
					superiorProfit.setStatus(UtilConstants.YES);
				}
				superiorProfit.setFromId(profit.getId());
				ret = profitService.insert(superiorProfit);
			}
			return ret;
		case SUPERIOR: // 一级合伙人
			LOGGER.info("[partner-rpc] - 插入一级合伙人收益");
			profit.setFee(BigDecimal.ZERO);
			profit.setType(ProfitSourceEnum.ADVERTISE_PROFIT.getCode());
			profit.setPlatformFee(BigDecimal.ZERO);
			profit.setRealAmount(realAmount);
			profit.setAdministratorId(partnerRelation.getAdministratorId());
			profit.setBusinessAdministratorId(businessRelation.getAdministratorId());
			profit.setSuperiorPartnerAdministratorId(partnerRelation.getSuperiorAdministratorId());
			profit.setCityAdministratorId(partnerRelation.getCityAdministratorId());
			profit.setSubstationAdministratorId(partnerRelation.getSubstationAdministratorId());
			profit.setCity(partnerRelation.getCity());
			if (null == profit.getUserType() || profit.getUserType() == UserTypeEnum.PLATFORM.getCode()) {
				profit.setStatus(UtilConstants.YES);
			} else {
				profit.setStatus(UtilConstants.YES);
			}
			return profitService.insert(profit);
		default: // TODO 其他
			break;
		}

		return -1;
	}
}
